from __init__ import db
import datetime


class Todo(db.Document):
    content = db.StringField(required=True, max_length=20)
    time = db.DateTimeField(default=datetime.datetime.now())
    status = db.IntField(default=0)
    mac = db.StringField(max_length=20)

    def to_json(self):
        return {
            'id': str(self.id),  # id需要转换为字符串格式
            'content': self.content,
            'mac': self.mac,
            'time': self.time,
            'status': self.status
        }


class User(db.Document):
    username = db.StringField(required=True, max_length=64)
    password = db.StringField(max_length=256)
    email = db.StringField(max_length=64)
    description = db.StringField(max_length=1024)

    # Flask-Login integration
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    # TypeError: ObjectId('552f41e56a85f00dd043406b') is not JSON serializable
    def get_id(self):
        return str(self.id)

    def __unicode__(self):
        return self.name
